#include "main.h"


/* 获取缓冲区的长度 */
#define BUF_SIZE    64

/*读写地址和擦除的扇区地址*/
#define  FLASH_WriteAddress     0x00000
#define  FLASH_ReadAddress      FLASH_WriteAddress
#define  FLASH_SectorToErase    FLASH_WriteAddress

     
/* 发送缓冲区初始化 */
uint8_t Tx_Buffer[BUF_SIZE] = "do not go gentle into that good night\r\n";
uint8_t Rx_Buffer[BUF_SIZE];


/*设备ID号*/
__IO uint32_t DeviceID = 0;
__IO uint32_t FlashID = 0;


/*主函数*/
int main(void)
{
    /*串口初始化*/
    usart_init(115200);
    
    /* 8M串行flash W25Q64初始化 */
    SPI_FLASH_Init();
    
    /* 获取 Flash Device ID */
    DeviceID = SPI_FLASH_ReadDeviceID();
    
	/* 获取 SPI Flash ID */
	FlashID = SPI_FLASH_ReadID();
    
    /* 检验 SPI Flash ID */
    if (FlashID == sFLASH_ID)
    {
		printf("\r\n detected flash W25Q64 !\r\n");
		
		/* 擦除将要写入的 SPI FLASH 扇区，FLASH写入前要先擦除 */
		// 这里擦除4K，即一个扇区，擦除的最小单位是扇区
		SPI_FLASH_SectorErase(FLASH_SectorToErase);	 

		/* 将发送缓冲区的数据写到flash中 */
		// 这里写一页，一页的大小为256个字节
		SPI_FLASH_BufferWrite(Tx_Buffer, FLASH_WriteAddress,BUF_SIZE);		
		printf("\r\ndata you write is:\r\n%s \r\t",Tx_Buffer);

		/* 将刚刚写入的数据读出来放到接收缓冲区中 */
		SPI_FLASH_BufferRead(Rx_Buffer, FLASH_ReadAddress,BUF_SIZE);
		printf("\r\ndata you read is:\r\n%s \r\n",Rx_Buffer);        
        
        
		/* 检查写入的数据与读出的数据是否相等 */
		if(strcmp((char *)Tx_Buffer,(char *)Rx_Buffer) == 0)
        {
            printf("\r\nok!!!\r\n");
        }
        else
        {
            printf("\r\nfailed!!!\r\n");
        }
    }
    
    while(1)
    {
    }
}


